package com.aliyun.messagemgt.application.service.Impl;


import com.aliyun.messagemgt.common.dto.send.PushChannelInfo;
import com.aliyun.messagemgt.application.service.PushChannelMap;
import com.aliyun.messagemgt.application.service.PushChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/1/29
 * @time:18:24
 */
@Component
public class PushChannelMapImpl implements PushChannelMap {

    private Map<Long, PushChannelInfo> channelIdMap;
    private Map<String, PushChannelInfo> channelCodeAndTenantMap;
    @Autowired
    private PushChannelService pushChannelService;
    @Override
    public void setChannelMap() {
        List<PushChannelInfo> channelInfoList = pushChannelService.listAllChannel();
        channelIdMap = new ConcurrentHashMap<>(channelInfoList.size() << 1);
        channelCodeAndTenantMap = new ConcurrentHashMap<>(channelInfoList.size() << 1);
        channelInfoList.forEach(channel -> {
            Long pushChannelId = channel.getPushChannelId();
            if (Objects.nonNull(pushChannelId)) {
                channelIdMap.put(pushChannelId, channel);
            }
            // 渠道编码-租户确定唯一性
            String channelCode = channel.getChannelCode();
            String tenantCode = channel.getTenantCode();
            if (StringUtils.isNotBlank(channelCode)) {
                channelCodeAndTenantMap.put(channelCode + tenantCode, channel);
            }
        });
    }

    @Override
    public List<PushChannelInfo> getChannelInfoByChannelCodeList(List<String> receiveChannelCodes, String tenantCode) {
        // 未初始化的情况
        if (channelCodeAndTenantMap == null) {
            setChannelMap();
        }
        List<PushChannelInfo> pushChannelInfoList = receiveChannelCodes.stream().map(
                channelCode -> {
                    String channelCodeAndTenant = channelCode + tenantCode;
                    PushChannelInfo pushChannelInfo = channelCodeAndTenantMap.get(channelCodeAndTenant);
                    // 获取不到，可能是由于新增了pushChannelInfo信息，所以重新再初始化一次
                    if (pushChannelInfo == null) {
                        setChannelMap();
                        pushChannelInfo = channelCodeAndTenantMap.get(channelCodeAndTenant);
                    }
                    return pushChannelInfo;
                }
        ).filter(obj -> Objects.nonNull(obj)).collect(Collectors.toList());
        return pushChannelInfoList;
    }

    @Override
    public PushChannelInfo getChannelInfoByChannelId(Long channelId) {
        // 未初始化的情况
        if (channelIdMap == null) {
            setChannelMap();
        }

        PushChannelInfo pushChannelInfo = channelIdMap.get(channelId);
        // 获取不到，可能是由于新增了pushChannelInfo信息，所以重新再初始化一次
        if (pushChannelInfo == null) {
            setChannelMap();
            pushChannelInfo = channelIdMap.get(channelId);
        }
        return pushChannelInfo;
    }


    @Override
    public PushChannelInfo getChannelInfoByChanneCode(String channelCode, String tenantCode) {
        // 未初始化的情况
        if (channelCodeAndTenantMap == null) {
            setChannelMap();
        }
        String channelCodeAndTenant = channelCode + tenantCode;
        PushChannelInfo pushChannelInfo = channelCodeAndTenantMap.get(channelCodeAndTenant);
        // 获取不到，可能是由于新增了pushChannelInfo信息，所以重新再初始化一次
        if (pushChannelInfo == null) {
            setChannelMap();
            pushChannelInfo = channelCodeAndTenantMap.get(channelCodeAndTenant);
        }
        return pushChannelInfo;
    }

    @Override
    public List<PushChannelInfo> getChannelInfoByChannelIdList(List<Long> channelIdList) {
        // 未初始化的情况
        if (channelIdMap == null) {
            setChannelMap();
        }
        List<PushChannelInfo> pushChannelInfoList = channelIdList.stream().map(
                channelId -> {
                    PushChannelInfo pushChannelInfo = channelIdMap.get(channelId);
                    // 获取不到，可能是由于新增了pushChannelInfo信息，所以重新再初始化一次
                    if (pushChannelInfo == null) {
                        setChannelMap();
                        pushChannelInfo = channelIdMap.get(channelId);
                    }
                    return pushChannelInfo;
                }
        ).collect(Collectors.toList());
        return pushChannelInfoList;
    }

    @Override
    public void addChannelCodeMapCache(PushChannelInfo pushChannelInfo) {
        if (Objects.nonNull(pushChannelInfo)) {
            channelCodeAndTenantMap.put(pushChannelInfo.getChannelCode() + pushChannelInfo.getTenantCode(), pushChannelInfo);
        }
    }

    @Override
    public void removeChannelCodeMapCache(String channelCode, String tenantCode) {
        String channelCodeAndTenant = channelCode + tenantCode;
        channelCodeAndTenantMap.remove(channelCodeAndTenant);
    }
}
